package com.imooc.trading_system.controller;


import com.google.code.kaptcha.Producer;
import com.imooc.trading_system.service.UserService;
import com.imooc.trading_system.utils.JsonData;
import com.imooc.trading_system.vo.user.LoginUserReqVO;
import com.imooc.trading_system.vo.user.RegisterReqVO;
import com.imooc.trading_system.vo.user.UpdateLoginUserInfoReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Feng zhaoxi
 * @since 2022-04-23
 */
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private Producer captchaProducer;

    /**
     * 用户登录
     *
     * @param loginUserReqVO
     * @return
     */
    @PostMapping("login")
    public JsonData login(@RequestBody LoginUserReqVO loginUserReqVO) {
        return userService.login(loginUserReqVO);
    }

    /**
     * 用户注册
     *
     * @param registerReqVO
     * @return
     */
    @PostMapping("register")
    public JsonData register(@RequestBody RegisterReqVO registerReqVO) {
        return userService.register(registerReqVO);
    }

    /**
     * 获取登录用户的信息
     *
     * @return
     */
    @PostMapping("get_login_user_info")
    public JsonData getLoginUserInfo() {
        return userService.getLoginUserInfo();
    }

    /**
     * 修改个人信息(除头像)
     *
     * @return
     */
    @PostMapping("update_login_user_info")
    public JsonData updateLoginUserInfo(@RequestBody UpdateLoginUserInfoReqVO updateLoginUserInfoReqVO) {
        return userService.updateLoginUserInfo(updateLoginUserInfoReqVO);
    }

    /**
     * 上传头像
     *
     * @param file
     * @return
     */
    @RequestMapping("upload_head_img")
    public JsonData uploadHeadImg(@RequestParam("file") MultipartFile file) {
        return userService.uploadHeadImg(file);
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    @PostMapping("get_date")
    public JsonData getDate() {
        return userService.getDate();
    }

    @GetMapping("get_kaptcha")
    public void getKaptcha(HttpServletRequest request, HttpServletResponse response) {
        String captchaText = captchaProducer.createText();
        log.info("图形验证码:{}", captchaText);

        BufferedImage bufferedImage = captchaProducer.createImage(captchaText);
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            log.error("获取图形验证码异常:{}", e);
        }
    }


}

